//
//  CYShareTool.m
//  CreditAddressBook
//
//  Created by LE on 16/1/15.
//  Copyright © 2016年 Lee. All rights reserved.
//

#import "CYShareTool.h"
#import "UMSocial.h"
#import "LEAlertView+LE.h"

@interface CYShareTool ()<UMSocialUIDelegate>
@property(nonatomic, copy) CYShareBlock block;
@end

@implementation CYShareTool
/**
 *  分享
 */
+ (void)shareImage:(UIImage *)shareImage controller:(UIViewController *)controller completion:(CYShareBlock)completion {
    CYShareTool *tool = [CYShareTool shared];
    [tool shareImage:shareImage controller:controller completion:completion];
}
+ (instancetype)shared {
    static CYShareTool *tool = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        tool = [[CYShareTool alloc] init];
    });
    return tool;
}
/**
 *  分享
 */
- (void)shareImage:(UIImage *)shareImage controller:(UIViewController *)controller completion:(CYShareBlock)completion {
    if (shareImage) {
        self.block = [completion copy];
//        UIImage *shareImage = [CYDialTool imageFromView:shareView];
        UIImageView *shareImageView = [[UIImageView alloc] initWithImage:shareImage];
        [LEAlertView showAlertViewWithButton:@"分享" cancelButton:@"取消" customView:shareImageView showInView:nil clickAtIndex:^(NSInteger index) {
            if (index == 1) {
                NSString *shareText = @"天下无赖";//分享内嵌文字
                //调用快速分享接口
                [UMSocialSnsService presentSnsIconSheetView:controller appKey:UMAppkey shareText:shareText shareImage:shareImage shareToSnsNames:nil delegate:self];
                [UMSocialData defaultData].extConfig.wxMessageType = UMSocialWXMessageTypeImage;
            }
        }];
    }
}
/**
 *  分享完成回调
 */
- (void)didFinishGetUMSocialDataInViewController:(UMSocialResponseEntity *)response {
    if (response.responseCode == UMSResponseCodeSuccess) {
        self.block(YES);
    } else {
        self.block(NO);
    }
    self.block = nil;
}
@end
